/*
 * Created on Feb 25, 2005
 */
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/**
 * @author Jesse Phillips
 */
public class Mousing /*ans bl*/implements MouseListener, MouseMotionListener /*ans ab*/{
	private Color drawingColor = Color.WHITE, bgColor = Color.BLACK;
	private int previousX, previousY;
	private Graphics myGraphics;
	private Tracker myTrack;
	private int drawWidth = 570;
	private int x, y, drop, alignRight, length, position;
	private int ovalWidth = 4, ovalHeight = 1;
	private int style = 0, thickness = 1;
	private boolean fill = true;
	
	private int panelX = 570, panelHeight = 400;
	
	public Mousing(Graphics g, Tracker t) {
		myGraphics = g;
		myTrack = t;
	}
	
	/*
	 *  Mouse Listener events
	 */
	public void mousePressed(MouseEvent e){
		myGraphics.setColor(drawingColor);
		if (e.getX() > drawWidth) {
			//Let a different function deal with it.
			atSidebar(e);
		} else {
			//drawing begins, save point
			previousX = e.getX();
			previousY = e.getY();
		}
	}
	
	public void mouseReleased(MouseEvent e) {
	/* You are to insert the proper content here */
	/*ans bl*/
		if (e.getX()< drawWidth){
			if(style == 2) {
				myGraphics.drawLine(previousX, previousY, e.getX(), e.getY());
				previousX = e.getX();
				previousY = e.getY();
			}
		}else {
			previousX = (drawWidth);
		}
	/*ans ab*/
	}
	
	public void mouseClicked(MouseEvent e) {
		if(style == 3) {
			ovalDraw(e);
		}else {
			dotDraw(e);
		}
	}
	
	public void mouseEntered(MouseEvent e) {
		
	}
	
	public void mouseExited(MouseEvent e) {
		
	}
	/*
	 * Mouse Motion listener events
	 */
	public void mouseDragged(MouseEvent e) {
		myGraphics.setColor(drawingColor);
		if(style == 2) {
			//Draw a line from the first poit held to the point released
			//Uses mouseReleased();
		}else if(style == 1) {
			//drawing are dots
			dotDraw(e);
		}else if(style == 3) {
			//drawing are dots
			ovalDraw(e);
		}else {
			if (e.getX()< drawWidth){
				//drawing continues
				myGraphics.drawLine(previousX, previousY, e.getX(), e.getY());
				previousX = e.getX();
			}else {
				previousX = drawWidth;
			}
			previousY = e.getY();
		}
	}
	
	
	/*
	 * other methods
	 */
	/**
	 * Set Variables
	 */
	public void setStyle(int s) {
		style = s;
	}
	
	public void setThick(int t) {
		thickness = t;
	}
	
	public void setFill() {
		if(fill) {
			fill = false;
		}else {
			fill = true;
		}
	}
	public void setBGColor(Color c) {
		bgColor = c;
		redraw();
	}
	
	/**
	 * @param e
	 */
	private void dotDraw(MouseEvent e) {
		if ((e.getX()+(thickness/2))< drawWidth){
			if(fill) {
				myGraphics.fillOval(e.getX()-(thickness/2),
						e.getY()-(thickness/2), 
						thickness, thickness);
			}else {
				myGraphics.drawOval(e.getX()-(thickness/2),
						e.getY()-(thickness/2), 
						thickness, thickness);
			}
		}
	}

	public void mouseMoved(MouseEvent e) {
		if (myTrack.isRedraw()) {
			drawSidebar();
		}
	}
	
	public void atSidebar(MouseEvent e) {
		if(e.getY() < (length+drop)) {
			drawingColor = Color.WHITE;
		}else if (e.getY() < ((length*2)+drop)) {
			drawingColor = Color.lightGray;
		}else if (e.getY() < ((length*3)+drop)) {
			drawingColor = Color.blue;
		}else if (e.getY() < ((length*4)+drop)) {
			drawingColor = Color.cyan;
		}else if (e.getY() < ((length*5)+drop)) {
			drawingColor = Color.green;
		}else if (e.getY() < ((length*6)+drop)) {
			drawingColor = Color.magenta;
		}else if (e.getY() < ((length*7)+drop)) {
			drawingColor = Color.yellow;
		}else if (e.getY() < ((length*8)+drop)) {
			drawingColor = Color.red;
		}else {
//			redraw field
			redraw();
		}
	}
	
	public void redraw() {
		//set up color choice rectangles
		x = myTrack.getWidth();
		y = myTrack.getHeight();
		myGraphics.setColor(bgColor);
		myGraphics.fillRect(0, 0, x, y);
		drawSidebar();
		
		
	}
	public void drawSidebar() {
		//Clear old panel
		myGraphics.setColor(bgColor);
		myGraphics.fillRect(panelX, 0, 30, panelHeight);
		
		x = myTrack.getWidth();
		y = myTrack.getHeight();
		
		drawWidth = (x-30);
		alignRight = (x-30);
		drop = 50;
		length = ((y-drop)/9);
		
		myGraphics.setColor(Color.WHITE);
		myGraphics.fillRect(alignRight, drop, 30, length);
		myGraphics.setColor(Color.lightGray);
		myGraphics.fillRect(alignRight, length+drop, 30, length);
		myGraphics.setColor(Color.BLUE);
		myGraphics.fillRect(alignRight, (length*2)+drop, 30, length);
		myGraphics.setColor(Color.CYAN);
		myGraphics.fillRect(alignRight, (length*3)+drop, 30, length);
		myGraphics.setColor(Color.green);
		myGraphics.fillRect(alignRight, (length*4)+drop, 30, length);
		myGraphics.setColor(Color.magenta);
		myGraphics.fillRect(alignRight, (length*5)+drop, 30, length);
		myGraphics.setColor(Color.yellow);
		myGraphics.fillRect(alignRight, (length*6)+drop, 30, length);
		myGraphics.setColor(Color.red);
		myGraphics.fillRect(alignRight, (length*7)+drop, 30, length);
		myGraphics.setColor(Color.white);
		
		panelX = alignRight;
		panelHeight = y;
	}
	public void ovalDraw(MouseEvent e) {
		int width = ovalWidth+thickness;
		int height = ovalHeight+thickness;
		if (e.getX()<drawWidth-(width/2)){
			if(fill) {
				myGraphics.fillOval(e.getX()-(width/2), e.getY()-
						(height/2), width, height);
			}else {
				myGraphics.drawOval(e.getX()-(width/2), e.getY()-
						(height/2), width, height);
			}
		}else {
			previousX = drawWidth;
		}
	}
}
